package org.luxor.cloud.authentication.component.captcha;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomListColorGenerator;
import com.octo.captcha.component.image.deformation.ImageDeformation;
import com.octo.captcha.component.image.deformation.ImageDeformationByFilters;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.FileDictionary;
import com.octo.captcha.component.word.wordgenerator.ComposeDictionaryWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;

import java.awt.*;
import java.awt.image.ImageFilter;

/**
 * 验证码图片引擎
 *
 * @author Mr.Yan  @date: 2021/1/8
 */
public class CaptchaEngine extends ListImageCaptchaEngine {

    private int imageWidth;
    private int imageHeight;
    private int fontSize;
    private int minWordLength;
    private int maxWordLength;

    public CaptchaEngine(int imageWidth, int imageHeight, int fontSize, int minWordLength, int maxWordLength) {
        this.imageWidth = imageWidth;
        this.imageHeight = imageHeight;
        this.fontSize = fontSize;
        this.minWordLength = minWordLength;
        this.maxWordLength = maxWordLength;
    }

    @Override
    protected void buildInitialFactories() {
        //随机文本的字典，这里是使用jcaptcha-1.0-all.jar中的文本字典，字典名称为toddlist.properties
        WordGenerator dictionnaryWords = new ComposeDictionaryWordGenerator(new FileDictionary("toddlist"));

        // word2image components
        Color[] colors = new Color[]{new Color(23, 170, 27), new Color(220, 34, 11),
                new Color(23, 67, 172)};
        RandomListColorGenerator rlcg = new RandomListColorGenerator(colors);
        TextPaster randomPaster = new DecoratedRandomTextPaster(minWordLength, maxWordLength, rlcg, new TextDecorator[]{});
        Font[] fonts = new Font[]{new Font("nyala", Font.BOLD, fontSize),
                new Font("Bell MT", Font.PLAIN, fontSize),
                new Font("Credit valley", Font.BOLD, fontSize)};
        FontGenerator font = new RandomFontGenerator(fontSize, fontSize, fonts);
        BackgroundGenerator background = new UniColorBackgroundGenerator(imageWidth, imageHeight, Color.white);

        ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[]{});
        ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[]{});
        ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[]{});

        WordToImage word2image = new DeformedComposedWordToImage(font, background, randomPaster,
                backDef, textDef, postDef);
        addFactory(new GimpyFactory(dictionnaryWords, word2image));
    }
}
